//
//  TSYVisitView.swift
//  TSYWeiBoDemo
//
//  Created by apple on 2016/11/24.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class TSYVisitView: UIView {
    
    var visitorInfo:[String:String]?{
        
        didSet{
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                return
            }
            
            if imageName == "" {
                return
            }
        }
        
        
    }

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        backgroundColor = UIColor.green
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
    }
    
    //旋转动画
    private func startAnimation(){
        
        let anim = CABasicAnimation(keyPath: "tranform.rotation")
        
        anim.toValue = 2*M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
         
        //TODO:此处是为了能够让动画持续   动画不删除 此处动画会绑定在动画涂层上
        anim.isRemovedOnCompletion = false;
        
    }
    
    
    /// 返回重新定义的图像
    ///
    /// - Parameters:
    ///   - image: 原始图片
    ///   - size: 想要的尺寸
    /// - Returns: 新图片
    func avatar(image:UIImage,size:CGSize,backColor:UIColor?) -> UIImage? {
        
        let rect = CGRect(origin: CGPoint(), size: size)
        
        ///size:绘图尺寸
        ///不透明？
        ///scale 屏幕分辨率 默认使用1.0 图像质量低的时候可以制定 0  ，会选择当前设备的分辨率
        UIGraphicsBeginImageContextWithOptions(size, true, 0)
        
        backColor?.setFill()
        
        UIRectFill(rect)
        ///裁剪圆形
        let path = UIBezierPath(ovalIn: rect)
        
        path.addClip()
        
        image.draw(in: rect)
        
        UIColor.darkGray.setStroke()
        
        path.lineWidth = 5;
        
        path.stroke()
        
        let result = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        
        return result
        
    }
    
}
